package com.hzqy.service.statistics;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.hzqy.web.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.hzqy.commons.utils.ConvertBeanUtil;
import com.hzqy.commons.utils.ExcelUtil;
import com.hzqy.commons.utils.PublicUtil;
import com.hzqy.commons.utils.ExcelUtil.MyEntry;
import com.hzqy.commons.utils.SystemConfigUtils;
import com.hzqy.persistence.dao.statistics.VisitDao;
import com.hzqy.service.core.GenericServiceBean;

@Service
public class VisitServiceBean extends GenericServiceBean<PmsUserVo, VisitDao, Integer>{
	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private VisitDao visitDao;

	@Override
	public VisitDao getEntityDao() {
		return visitDao;
	}

	public List<PmsZTreeVo> selectConfUrlTree(StatConfUrlVo statConfUrlVo) {
		List<PmsZTreeVo> ptvList = new ArrayList<PmsZTreeVo>();
		List<StatConfUrlVo> prvList = visitDao.selectConfUrl(statConfUrlVo);
		for(int i=0; i<prvList.size();i++){
			PmsZTreeVo ptv = new PmsZTreeVo();
			ptv.setId(prvList.get(i).getF_dvum_id()+"");
			ptv.setName(prvList.get(i).getF_dvum_name());
			ptvList.add(ptv);
		}
		return ptvList;
	}

	public Map<String,Object> selectVisitList(StatDataVisitVo statDataVisitVo, int curPage,int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = visitDao.selectVisitContentCount(statDataVisitVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_dv_urlid", statDataVisitVo.getF_dv_urlid());
			map.put("f_dv_urltype", statDataVisitVo.getF_dv_urltype());
			map.put("f_dv_type", statDataVisitVo.getF_dv_type());
			map.put("f_dv_areaid", statDataVisitVo.getF_dv_areaid());
			map.put("f_ui_resolution", statDataVisitVo.getF_ui_resolution());
			map.put("beginDate", statDataVisitVo.getBeginDate());
			map.put("endDate", statDataVisitVo.getEndDate());
			map.put("monthDate", statDataVisitVo.getMonthDate());
			map.put("weekDate", statDataVisitVo.getWeekDate());
			map.put("f_dvum_url", statDataVisitVo.getF_dvum_url());
			map.put("f_dv_orderType", statDataVisitVo.getF_dv_orderType());
			map.put("f_dv_groupType", statDataVisitVo.getF_dv_groupType());
			map.put("f_dv_orgId", statDataVisitVo.getF_dv_orgId());
			map.put("f_dv_orgGroup", statDataVisitVo.getF_dv_orgGroup());
			List<StatDataVisitVo> results = visitDao.selectVisitList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<StatDataVisitVo>());
		}
		return map;
	}

	public Map<String,Object> selectExcelList(StatDataVisitVo statDataVisitVo) {
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = visitDao.selectExcelCount(statDataVisitVo);
		map.put("total", rowCount);
		if(rowCount > 0) {
			map.put("f_dv_urlid", statDataVisitVo.getF_dv_urlid());
			map.put("f_dv_type", statDataVisitVo.getF_dv_type());
			map.put("f_dv_areaid", statDataVisitVo.getF_dv_areaid());
			map.put("f_dv_urltype", statDataVisitVo.getF_dv_urltype());
			map.put("f_ui_resolution", statDataVisitVo.getF_ui_resolution());
			map.put("beginDate", statDataVisitVo.getBeginDate());
			map.put("endDate", statDataVisitVo.getEndDate());
			map.put("monthDate", statDataVisitVo.getMonthDate());
			map.put("weekDate", statDataVisitVo.getWeekDate());
			map.put("f_dvum_url", statDataVisitVo.getF_dvum_url());
			map.put("f_dv_groupType", statDataVisitVo.getF_dv_groupType());
			map.put("f_dv_orgId", statDataVisitVo.getF_dv_orgId());
			map.put("f_dv_orgGroup", statDataVisitVo.getF_dv_orgGroup());
			List<StatDataVisitVo> results = visitDao.selectExcelList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<StatDataVisitVo>());
		}
		return map;
	}

	public Map<String,Object> findConfUrlList(StatConfUrlVo statConfUrlVo, int curPage, int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = visitDao.findConfUrlCount(statConfUrlVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("f_dvum_name", statConfUrlVo.getF_dvum_name());
			map.put("f_dvum_url", statConfUrlVo.getF_dvum_url());
			map.put("f_dvum_type", statConfUrlVo.getF_dvum_type());
			map.put("f_dvum_status", statConfUrlVo.getF_dvum_status());
			map.put("beginDate", statConfUrlVo.getBeginDate());
			map.put("endDate", statConfUrlVo.getEndDate());
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			List<StatConfUrlVo> results = visitDao.findConfUrlList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<StatConfUrlVo>());
		}
		return map;
	}

	public int deleteConfUrl(int f_dvum_id) {
		int flag=visitDao.deleteConfUrl(f_dvum_id);
		return flag;
	}

	public int insertConfUrl(StatConfUrlVo statConfUrlVo) {
		int flag = 0;
		if(statConfUrlVo==null || statConfUrlVo.getF_dvum_name()==null) {
			flag = -1;
		}
		if(flag ==0) {
			flag=visitDao.insertConfUrl(statConfUrlVo);
		}
		return flag;
	}

	public StatConfUrlVo findConfUrl(int f_dvum_id) {
		return visitDao.findConfUrl(f_dvum_id);
	}

	public int updateConfUrl(StatConfUrlVo statConfUrlVo) {
		int flag = 0;
		if(statConfUrlVo==null || statConfUrlVo.getF_dvum_name()==null) {
			flag = -1;
		}
		if(flag ==0) {
			flag=visitDao.updateConfUrl(statConfUrlVo);
		}
		return flag;
	}

	public StringBuffer createPVUVExcel(String runtime,String f_dv_orgGroup,int f_dv_areaid) {
		StringBuffer str = new StringBuffer();
		//获取文件流
		String filePath = SystemConfigUtils.getSysConfig("sysutil_downFile");
		String uploadPath = filePath+"免费栏目.xlsx";
		String create_path = filePath+"免费栏目数据.xlsx";
		InputStream is=null;
		try {
			is = new FileInputStream(uploadPath);
			LOG.debug("解析excel文件");
			List<Map<String, Object>> list = ExcelUtil.parseExcel(is, ExcelUtil.ALLOWED_EMPTY_MODE,
					new String[] {});
			List<StatDataVisitVo> data = new ArrayList<StatDataVisitVo>();
			for(int i=0;i<list.size();i++){
				try {
					Map<String, Object> m = list.get(i);
					StatDataVisitVo dataVisitVo =  ConvertBeanUtil.getBeanFromMap(m, StatDataVisitVo.class, StatDataVisitVo.class.getName());
					if(runtime ==null||runtime == ""){
						String dateTime = PublicUtil.getCurDateTime("YYYY-MM-dd");
						runtime = dateTime;
					}
					dataVisitVo.setF_dv_date(runtime);
					dataVisitVo.setF_dv_orgGroup(f_dv_orgGroup);
					dataVisitVo.setF_dv_areaid(f_dv_areaid);
					List<StatDataVisitVo> statDataVisitVo = visitDao.selectUVPVByUrl(dataVisitVo);
					int bqPv=0;
					int bqUv=0;
					if(!statDataVisitVo.isEmpty()){
						for(int j=0;j<statDataVisitVo.size();j++){
							StatDataVisitVo newDataVisitVo = new StatDataVisitVo();
							newDataVisitVo.setF_dv_urlname(dataVisitVo.getF_dv_urlname());
							newDataVisitVo.setF_dvum_url(dataVisitVo.getF_dvum_url());
							newDataVisitVo.setF_dv_date(runtime);
							newDataVisitVo.setF_dv_orgGroup(f_dv_orgGroup);
							newDataVisitVo.setF_dv_areaid(f_dv_areaid);
							bqPv=statDataVisitVo.get(j).getF_dv_pvcount();
							bqUv=statDataVisitVo.get(j).getF_dv_uvcount();
							newDataVisitVo.setF_dv_pvcount(bqPv);
							newDataVisitVo.setF_dv_uvcount(bqUv);
							newDataVisitVo.setF_dv_areaname(statDataVisitVo.get(j).getF_dv_areaname());
							data.add(newDataVisitVo);
						}
					}
//					if(statDataVisitVo != null){
//						bqPv=statDataVisitVo.getF_dv_pvcount();
//						bqUv=statDataVisitVo.getF_dv_uvcount();
//						dataVisitVo.setF_dv_pvcount(bqPv);
//						dataVisitVo.setF_dv_uvcount(bqUv);
//						dataVisitVo.setF_dv_areaname(statDataVisitVo.getF_dv_areaname());
//					}

					//data.add(dataVisitVo);

				} catch (Exception e) {
					LOG.error(e.toString());
				}
			}
			LOG.debug("开始生成excel");
			List<MyEntry<String, String>> pvuvExcel = new ArrayList<MyEntry<String, String>>();
			pvuvExcel.add(new MyEntry<String,String>("f_dv_urlname","栏目页面"));
			pvuvExcel.add(new MyEntry<String, String>("f_dvum_url","具体路径"));
			pvuvExcel.add(new MyEntry<String, String>("f_dv_pvcount", "PV"));
			pvuvExcel.add(new MyEntry<String, String>("f_dv_uvcount", "UV"));
			pvuvExcel.add(new MyEntry<String, String>("f_dv_date", "操作时间"));
			pvuvExcel.add(new MyEntry<String, String>("f_dv_orgGroup", "组织类别"));
			pvuvExcel.add(new MyEntry<String, String>("f_dv_areaname", "地市"));
			ExcelUtil.generateExcel(data, pvuvExcel, create_path);
			str.append("生成pvuv成功");
		} catch (Exception e) {
			LOG.debug("生成pvuv失败");
			str.append("生成pvuv失败"+e);
		}
		finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
				}
			}
		}
		return str;
	}

	public String addPVUVExcelService(HttpServletRequest request,String model,MultipartHttpServletRequest multipart) {
		StringBuffer result = new StringBuffer();
		String filePath = SystemConfigUtils.getSysConfig("sysutil_downFile");
		CommonsMultipartFile file = (CommonsMultipartFile) multipart.getFile("uploadify");
		String path = filePath+model;
		File uploadFile = new File(path);

		try {
			LOG.debug("开始上传");
			file.transferTo(uploadFile);

			result.append("导入数据成功");
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("上传文件异常：",e);
			//result.append("导入数据失败" + e.getMessage());
		}
		return result.toString();
	}

	public Map<String,Object> selectPlayRecordList(StatDataPlayRecordVo statDataPlayRecordVo, int curPage,int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = visitDao.selectPlayRecordCount(statDataPlayRecordVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_sdpr_type", statDataPlayRecordVo.getF_sdpr_type());
			map.put("f_sdpr_contentname", statDataPlayRecordVo.getF_sdpr_contentname());
			map.put("f_sdpr_serverid", statDataPlayRecordVo.getF_sdpr_serverid());
			map.put("f_sdpr_orgid", statDataPlayRecordVo.getF_sdpr_orgid());
			map.put("f_sdpr_contenttype", statDataPlayRecordVo.getF_sdpr_contenttype());
			map.put("beginDate", statDataPlayRecordVo.getBeginDate());
			map.put("endDate", statDataPlayRecordVo.getEndDate());
			map.put("monthDate", statDataPlayRecordVo.getMonthDate());
			map.put("weekDate", statDataPlayRecordVo.getWeekDate());
			List<StatDataPlayRecordVo> results = visitDao.selectPlayRecordList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<StatDataVisitVo>());
		}
		return map;
	}

	public Map<String,Object> selectSourcePathList(StatDataSearchPlayVo statDataSearchPlay, int curPage,int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = visitDao.selectSourcePathCount(statDataSearchPlay);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_sp_date", statDataSearchPlay.getF_sp_date());
			map.put("f_sp_serverid", statDataSearchPlay.getF_sp_serverid());
			List<StatDataPlayRecordVo> results = visitDao.selectSourcePathList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<StatDataVisitVo>());
		}
		return map;
	}

	public Map<String,Object> selectMerchantList(StatDataMerchantVo statDataMerchantVo, int curPage,int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = visitDao.selectMerchantCount(statDataMerchantVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("beginDate", statDataMerchantVo.getBeginDate());
			map.put("endDate", statDataMerchantVo.getEndDate());
			map.put("f_sdm_orgId", statDataMerchantVo.getF_sdm_orgId());
			List<StatDataMerchantVo> results = visitDao.selectMerchantList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<StatDataMerchantVo>());
		}
		return map;
	}

	public Map<String,Object> selectAdvertisingList(StatDataAdvertisingStatisticsVo statDataAdvertisingStatisticsVo, int curPage, int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = visitDao.selectAdvertisingCount(statDataAdvertisingStatisticsVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("beginDate", statDataAdvertisingStatisticsVo.getBeginDate());
			map.put("endDate", statDataAdvertisingStatisticsVo.getEndDate());
			map.put("f_sdas_caId", statDataAdvertisingStatisticsVo.getF_sdas_caId());
			map.put("f_sdas_caName", statDataAdvertisingStatisticsVo.getF_sdas_caName());
			map.put("f_sdas_areaid", statDataAdvertisingStatisticsVo.getF_sdas_areaid());
			map.put("f_sdas_resolution", statDataAdvertisingStatisticsVo.getF_sdas_resolution());
			map.put("f_sdas_date", statDataAdvertisingStatisticsVo.getF_sdas_date());
			map.put("f_sdas_type", statDataAdvertisingStatisticsVo.getF_sdas_type());
			List<StatDataAdvertisingStatisticsVo> results = visitDao.selectAdvertisingList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<StatDataAdvertisingStatisticsVo>());
		}
		return map;
	}
}
